package com.gjintel.framework.holidayset.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gjintel.business.webtypemanage.model.WebTypeInfo;
import com.gjintel.framework.cache.InitCacheData;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.holidayset.model.HolidaySet;
import com.gjintel.framework.holidayset.service.IHolidaySetService;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;

@RequestMapping("/framework/holidayset")
@Controller
public class HolidaySetController {

	@Autowired
	private IHolidaySetService holidaySetService ;

	/**
	 * @description 保存节假日信息
	 * @author lpshen
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session, WebTypeInfo articmanage,HttpServletRequest request) throws Exception {
		User user = (User) session.getAttribute("loginUser");
		String startDate = request.getParameter("startTime");
		String endDate = request.getParameter("endTime");
		String descript = request.getParameter("descript");
	//	String type = request.getParameter("type");		
		String year_s = startDate.substring(0, 4);
		Integer hoilday_type = Integer.valueOf(request.getParameter("hoilday_type"));
		
		List<Date> allDates = holidaySetService.getDays(startDate, endDate);  //获取公休日时间
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy");
		//List<String> dates = holidaySetService.findAllByYear(year_s);
		
		holidaySetService.saveWeekendsOfYear(year_s, user.getName()); //插入所有双休日
		if (hoilday_type==1) {
			//List<String> dates2 = holidaySetService.findAllByYear(year_s);
			List<String> dates2 =holidaySetService.findByYear(year_s);
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.SECOND, 1);
			for (Date date : allDates) {
				if (!dates2.contains(sdf1.format(date))) {
					HolidaySet holidaySet = new HolidaySet();
					holidaySet.setAt_day(sdf1.format(date));
					holidaySet.setAt_year(sdf2.format(date));
					holidaySet.setDescript(descript);
					holidaySet.setUpdateTime(cal.getTime());
					holidaySet.setCreate_name(user.getName());
					holidaySet.setHoilday_type(hoilday_type);
					holidaySetService.add(holidaySet);
					LogUtil.addLog(new LogInfo("添加节假日信息", user.getId(), user.getName(), String.valueOf(LogType.ADD.ordinal()),
							holidaySet.getClass().getSimpleName(), holidaySet.getId()));
				}else{
					//根据date 获取数据库中已经存在的HolidaySet
					HolidaySet holidaySet=holidaySetService.getHolidaySetbyDate(sdf1.format(date));
					holidaySet.setCreate_name(user.getName());
					holidaySet.setDescript(descript);
					holidaySet.setUpdateTime(cal.getTime());
					holidaySet.setHoilday_type(hoilday_type);
					this.holidaySetService.saveOrUpdate(holidaySet);
					LogUtil.addLog(new LogInfo("添加节假日信息", user.getId(), user.getName(), String.valueOf(LogType.ADD.ordinal()),
					holidaySet.getClass().getSimpleName(), holidaySet.getId()));
				}
			}
			return Constant.SAVE_SUCCESS;
		} else if (hoilday_type==2) {
			List<String> dates = holidaySetService.findByYear(year_s);
			for (Date date : allDates) {
				if (dates.contains(sdf1.format(date))) {
					holidaySetService.updateByDate((sdf1.format(date)), user.getName(),descript);
				}
			}
			return Constant.SAVE_SUCCESS;	
		} else {
			return Constant.SAVE_UN_SUCCESS;
		}
	}

	/**
	 * @description 获得节假日列表
	 * @author lpshen
	 * @param page
	 * @param holidaySet
	 * @param sort
	 * @return JSONObject
	 */
	@RequestMapping("/list")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject list(Page page, HolidaySet holidaySet, Sort sort) {
		this.holidaySetService.findAll(page, holidaySet, sort);
		return CommUtil.getPageJSONData(page, true);
	}

	/**
	 * @description 根据选择的记录进行删除数据
	 * @author lpshen
	 * @param session
	 * @param deleteIds
	 * @return
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public String remove(HttpSession session, String deleteIds) {
		User user = (User) session.getAttribute("loginUser");
		this.holidaySetService.removeData(deleteIds, user);
		return Constant.DEL_SUCCESS;
	}
}
